package com.poetry.controller;

import com.poetry.model.Poem;
import com.poetry.model.dto.PoemDTO;
import com.poetry.service.CategoryService;
import com.poetry.service.PoemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/poems")
public class PoemController {
    
    @Autowired
    private PoemService poemService;
    
    @Autowired
    private CategoryService categoryService;
    
    @GetMapping
    public String list(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Long category,
            @RequestParam(defaultValue = "latest") String sort,
            Model model) {
        
        Page<?> poemPage = poemService.findAll(page - 1, keyword, category, sort);
        
        model.addAttribute("title", "标题");
        model.addAttribute("poems", poemPage.getContent());
        model.addAttribute("currentPage", page);
        model.addAttribute("totalPages", poemPage.getTotalPages());
        model.addAttribute("categories", categoryService.findAll());
        return "poem/list";
    }
    
    @GetMapping("/{id}")
    public String detail(@PathVariable Long id, Model model) {
        model.addAttribute("poem", poemService.findById(id));
        return "poem/detail";
    }
    
    @GetMapping("/create")
    public String create(Model model) {
        model.addAttribute("title", "����ʫ�� - ʫ��ƽ̨");
        model.addAttribute("categories", categoryService.findAll());
        return "poem/create";
    }
    
    @GetMapping("/category/{categoryId}")
    public String listByCategory(
            @PathVariable Long categoryId,
            @RequestParam(defaultValue = "1") int page,
            Model model) {
        
        Page<?> poemPage = poemService.findByCategory(categoryId, page - 1);
        
        model.addAttribute("category", categoryService.findById(categoryId));
        model.addAttribute("poems", poemPage.getContent());
        model.addAttribute("currentPage", page);
        model.addAttribute("totalPages", poemPage.getTotalPages());
        return "poem/list";
    }
    
    @PostMapping
    public String createPoem(@ModelAttribute PoemDTO poemDTO, Model model) {
        poemService.create(poemDTO);
        model.addAttribute("message", "ʫ�ʷ����ɹ���");
        return "redirect:/poems";
    }
    
    @PutMapping("/{id}")
    public String updatePoem(@PathVariable Long id, @ModelAttribute PoemDTO poemDTO, Model model) {
        poemService.update(id, poemDTO);
        model.addAttribute("message", "ʫ�ʸ��³ɹ���");
        return "redirect:/poems";
    }
    
    @DeleteMapping("/{id}")
    public String deletePoem(@PathVariable Long id, Model model) {
        poemService.delete(id);
        model.addAttribute("message", "ʫ��ɾ���ɹ���");
        return "redirect:/poems";
    }
} 